package modules.manager
{
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Dictionary;

	public class SoundManager
	{

		[Embed("../../../asset/sound/mouseOver.mp3")]
		private var mouseOverCls:Class;

		[Embed("../../../asset/sound/mouseClick.mp3")]
		private var mouseClickCls:Class;

		[Embed("../../../asset/sound/drawTile.mp3")]
		private var drawTileCls:Class;

		[Embed("../../../asset/sound/tileClick.mp3")]
		private var discardTileCls:Class;

		[Embed("../../../asset/sound/specialTile.mp3")]
		private var specialTileCls:Class;

		[Embed("../../../asset/sound/loss.mp3")]
		private var lossCls:Class;

		[Embed("../../../asset/sound/chooseTile.mp3")]
		private var chooseTileCls:Class;

		[Embed("../../../asset/sound/bomb.mp3")]
		private var bombCls:Class;

		[Embed("../../../asset/sound/numberTimerSound.mp3")]
		private var numTimerCls:Class;

		[Embed("../../../asset/sound/chips.mp3")]
		private var chipsCls:Class;

		[Embed("../../../asset/sound/diceCls.mp3")]
		private var diceCls:Class;

		[Embed("../../../asset/sound/jackpot.mp3")]
		private var jackpotCls:Class;

		public static var instance:SoundManager;

		[Bindable]
		public static var enabled:Boolean = true;

		private var _mouseOverSound:Sound;

		private var _mouseClickSound:Sound;

		private var _drawTileSound:Sound;

		private var _winJackpotSound:Sound;

		private var _discardTileSound:Sound;

		private var _specialTileSound:Sound;

		private var _lossSound:Sound;

		private var _chooseTileSound:Sound;

		private var _bombSound:Sound;

		private var _numberTimerSound:Sound;

		private var _chipsSound:Sound;

		private var _diceSound:Sound;

		private var _showNumberTimer_channel:SoundChannel;

		private var _gender:String = "female";

		public function set gender(isFemale:Boolean):void
		{
			if (isFemale)
			{
				_gender = "female";
			}
			else
			{
				_gender = "male";
			}
		}

		public static function initSoundManager():SoundManager
		{
			if (instance == null)
			{
				instance = new SoundManager();
				instance.initSoundEffect();
			}

			return instance;
		}

		public function initSoundEffect():void
		{
			_mouseOverSound = new mouseOverCls as Sound;
			_mouseClickSound = new mouseClickCls as Sound;
			_drawTileSound = new drawTileCls as Sound;
			_discardTileSound = new discardTileCls as Sound;
			_specialTileSound = new specialTileCls as Sound;
			_lossSound = new lossCls as Sound;
			_chooseTileSound = new chooseTileCls as Sound;
			_bombSound = new bombCls as Sound;
			_numberTimerSound = new numTimerCls as Sound;
			_chipsSound = new chipsCls as Sound;
			_diceSound = new diceCls as Sound;
			_winJackpotSound = new jackpotCls as Sound;

			UIAllRefer.stage.addEventListener(MouseEvent.MOUSE_OVER, mouseHandler);
			UIAllRefer.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
		}

		private function mouseHandler(event:MouseEvent):void
		{
			if (!enabled)
			{
				return;
			}
//			if (MCToButton.isButton(event.target))
//			{
//				if (event.type == MouseEvent.MOUSE_OVER)
//				{
//					_mouseOverSound.play();
//				}
//				else if (event.type == MouseEvent.MOUSE_DOWN)
//				{
//					_mouseClickSound.play();
//				}
//			}
		}

		public function drawTileSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_drawTileSound.play();
		}

		public function winJackpotSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_winJackpotSound.play();
		}

		public function discardTileSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_discardTileSound.play();
		}

		public function promptSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_specialTileSound.play();
		}

		public function chooseTilePlay():void
		{
			if (!enabled)
			{
				return;
			}
			_chooseTileSound.play();
		}

		public function lossSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_lossSound.play();
		}

		public function bombSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_bombSound.play();
		}

		private var numverTimerChannelDic:Dictionary = new Dictionary();

		/**
		 * 控制倒计时声音
		 * @param play
		 * @param tableId
		 *
		 */
		public function numTimerSoundPlay(play:Boolean, tableId:*):void
		{
			if (!enabled)
			{
				return;
			}
			var numverTimerChannel:SoundChannel;
			numverTimerChannel = numverTimerChannelDic[tableId.toString()];
			if (play)
			{
				numverTimerChannel && numverTimerChannel.stop();

				numverTimerChannel = _numberTimerSound.play(0, 15);

				numverTimerChannelDic[tableId.toString()] = numverTimerChannel;
			}
			else if (numverTimerChannel)
			{
				numverTimerChannel = numverTimerChannelDic[tableId.toString()];
				numverTimerChannel && numverTimerChannel.stop();
			}
		}

		public function chipsSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_chipsSound.play();
		}

		public function diceSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			_diceSound.play();
		}

		public function winSoundPlay():void
		{
			if (!enabled)
			{
				return;
			}
			var sound:Sound = SoundZHCN.getSound(_gender + "_win");
			if (sound != null)
			{
				sound.play();
			}
		}

		public function tileSoundPlay(type:int, value:int):void
		{
			if (!enabled)
			{
				return;
			}
			var sound:Sound = getSoundByCardTypeAndValue(type, value);
			if (sound != null)
			{
				sound.play();
			}
		}

		private static var typeValues:Array = ["bam", "ccr", "cha", "east", "west", "south", "north", "red", "white", "green", "joker"];

		private function getSoundByCardTypeAndValue(type:int, value:int):Sound
		{
			var soundName:String = typeValues[type];
			if (type < 3)
			{
				soundName += value;
			}
			soundName = _gender + "_" + soundName;
			trace(" soundName ---> " + soundName);
			var sound:Sound = SoundZHCN.getSound(soundName);
			return sound;
		}

		public function actionSoundPlay(action:String):void
		{
			if (!enabled)
			{
				return;
			}
			switch (action)
			{
				case "raise":
				case "check":
				case "call":
				case "fold":
				case "bet":
					var sound:Sound = SoundZHCN.getSound(action);
					if (sound != null)
					{
						sound.play();
					}
					break;
				default:
					break;
			}
		}
	}
}
